# main menu
(class dd_world_menu dd_world
	(group

	# 3 buttons
	(def (array dd_meshTexture 3) obj)

	(def int tileCount)
	(def (array dd_matrix 3) tile)

	(def int selection)

	# open/close animation
	(def float shrinkAnimation)
	(def int opening)
	(def int fromIntro)

	# scale to fit all screen sizes
	(def float scale)

	(def dd_meshColour selectionMesh)
	(def dd_matrix selectionMatrix)

	(def dd_matrix unselectedMatrix)

	# game's title
	(def dd_meshColour title)
	(def float titleAnimation)

	(function create (group)
		(group

		(dd_clearColour 0.1 0.1 0.1 1)

		(for (def int i 0) (< i 3) (= i (+ i 1))
			(group
			(dd_meshTexture_create this.obj[i])
			)
		)
		(this.obj[0].load "menu_game.ply")
		(this.obj[1].load "menu_cog.ply")
		(this.obj[1].loadTexture "menu_cog.bmp")
		(this.obj[2].load "menu_exit.ply")

		(= this.tileCount 3)
		(for (def int i 0) (< i this.tileCount) (= i (+ i 1))
			(group
			(dd_matrix_identity this.tile[i])
			(dd_matrix_translates this.tile[i] 0 (- 2 (* 2 i)) 0)
			)
		)

		#(dd_matrix_translates this.tile[0] 0 (/ (dd_screen_height_get 10) 2) 0)
		#(dd_matrix_translates this.tile[0] (/ (dd_screen_width_get 10) 2) 0 0)

		(= this.selection 0)

		(= this.shrinkAnimation 0)
		(= this.opening 1)
		(= this.fromIntro 0)

		(= this.scale 1)

		(dd_meshColour_create this.selectionMesh)
		(this.selectionMesh.load "selection.ply")

		(dd_matrix_identity this.selectionMatrix)

		(dd_matrix_identity this.unselectedMatrix)

		# game's title
		(dd_meshColour_create this.title)
		(this.title.load "title.ply")
		(= this.titleAnimation 0)

		)
	)

	(function resize (group)
		(group
		#(dd_matrix_translates this.tile[0] 0 (/ (dd_screen_height_get 10) 2) 0)
		#(dd_matrix_translates this.tile[1] (/ (dd_screen_width_get 10) 2) 0 0)

		(def float height)
		(= height (/ (dd_screen_height_get 10) 10)) # last number is the height of the object
		(def float width)
		(= width (/ (dd_screen_width_get 10) 4)) # last number is the width of the object

		(if (< width height)
			(= this.scale width)
		# else
			(= this.scale height)
		)

		)
	)

	# update - add rotation
	(function update (group)
		(group

		(for (def int i 0) (< i 3) (= i (+ i 1))
			(group

			(if (== i this.selection)
				(group
				(dd_matrix_rotate_y this.tile[i] (dd_math_dec2rad 1))
				)

				(group
				(dd_matrix_approach_rs this.tile[i] this.unselectedMatrix 0.3)
				)
			)

			)
		)

		# selection is rotating constantly
		(dd_matrix_rotate_y this.selectionMatrix (dd_math_dec2rad 5))
		(dd_matrix_translates this.selectionMatrix
			0
			(+ (dd_matrix_y this.selectionMatrix) (* (- (dd_matrix_y this.tile[this.selection]) (dd_matrix_y this.selectionMatrix)) 0.3))
			0
		)

		# opening animation
		(if (&& (< this.shrinkAnimation 1) this.opening)
			(group
			(= this.shrinkAnimation (+ this.shrinkAnimation (dd_math_maxf (* (- 1 this.shrinkAnimation) 0.3) 0.05)))
			(if (> this.shrinkAnimation 1) (= this.shrinkAnimation 1))
			(if this.fromIntro
				(dd_clearColour (* this.shrinkAnimation 0.1) (* this.shrinkAnimation 0.1) (* this.shrinkAnimation 0.1) 1)
				(dd_clearColour 0.1 0.1 0.1 1)
			)
			)
		# closing animation
		(&& (> this.shrinkAnimation 0) (== this.opening 0))
			(group

			(= this.shrinkAnimation (- this.shrinkAnimation (dd_math_maxf (* (- 1 this.shrinkAnimation) 0.3) 0.05)))

			# make selection
			(if (<= this.shrinkAnimation 0)
				(group
				(if (== this.selection 0)
					(dd_world_set dd_world_level_select)
				(== this.selection 2)
					(= dd_flag_exit 1) # exit game
				)
				)
			)

			)
		) # opening/closing animation

		(= this.titleAnimation (+ this.titleAnimation 0.001))
		(if (> this.titleAnimation 1) (= this.titleAnimation (- this.titleAnimation 1)))
		)
	)

	# draw
	# zoom-out so the world is visible
	# rotate the world
	# draw the cube
	(function draw (group)
		(group

		(glTranslatef 0 0 -10)

		(for (def int i 0) (< i this.tileCount) (= i (+ i 1))
			(group
			(glPushMatrix)
			(glScalef this.scale this.scale this.scale)
			(if (== this.selection i)
				(group
				#(glTranslatef 0 0 0.5)
				(glPushMatrix)
				(glMultMatrixf this.selectionMatrix)
				(this.selectionMesh.draw)
				(glPopMatrix)
				#(glScalef 1.1 1.1 1.1)
				)
			)
			(glMultMatrixf this.tile[i])
			(glScalef this.shrinkAnimation this.shrinkAnimation this.shrinkAnimation)
			(this.obj[i].draw)
			(glPopMatrix)
			)
		)

		(glTranslatef 0 0 -10)
		(glScalef 5 5 5)

		(if (< this.titleAnimation 0.5)
			(glScalef (+ 1 this.titleAnimation) (+ 1 this.titleAnimation) (+ 1 this.titleAnimation))
		# else
			(glScalef (- 2 this.titleAnimation) (- 2 this.titleAnimation) (- 2 this.titleAnimation))
		)
		(this.title.draw)

		)
	)

	(function key_input (group char key)
		(group

		(if (== key 's')
			(group
			(= this.selection (+ this.selection 1))
			(if (>= this.selection this.tileCount) (= this.selection 0))
			)
		(== key 'w')
			(group
			(= this.selection (- this.selection 1))
			(if (< this.selection 0) (= this.selection (- this.tileCount 1)))
			)
		(== key ' ')
			(group
			(= this.opening 0)
			)
		)

		)
	)

	(function clean (group)
		(group

		(this.obj[0].clean)
		(this.obj[1].clean)
		(this.obj[2].clean)

		(dd_world_clean this)

		)
	)

	)
) # main world
